/*
 * Copyright (c) 2023-2025 110476592@qq.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @file shape_picker.cpp
 * @brief 截图工具 
 * @author 110476592@qq.com 
 * @note   承接Qt项目外包定制开发。长期接收C++项目实训学徒,联系QQ：110476592。
 */

 
#include "shape_picker.h"
#include <QDebug>
#include <QEvent>
#include <QHBoxLayout>
#include <QPainter>
#include <QRect>
#include <QScreen>
#include <QVBoxLayout>
#include <QVariant>
#include <QGridLayout>
#include "shape_item.h"

#include <common/draw_context.h>

ShapePicker::ShapePicker(QWidget *parent)
    : ELPopover(parent) {
    setProperty("form", "absdialog");
    this->setObjectName("shapePicker");
    this->setStyleSheet(
        "shapePicker#colorPicker{padding:10px;background-color:rgba(237, "
        "237, 237,1);border-radius:4px;}");

    m_shapes.insert(PainterType::PENCIL,IconFont::FA_CURVE_LINE);
    m_shapes.insert(PainterType::ARROW_LINE ,IconFont::FA_ARROW);
    m_shapes.insert(PainterType::RECT,IconFont::FA_RECT);
    m_shapes.insert(PainterType::FILL_RECT,IconFont::FA_RECT);

    m_shapes.insert(PainterType::CIRCLE,IconFont::FA_CIRCLE);
    m_shapes.insert(PainterType::BLUR ,IconFont::FA_BLUR);
    m_shapes.insert(PainterType::LINE ,IconFont::FA_LINE);
    m_shapes.insert(PainterType::MARKER ,IconFont::FA_LINE);




    createWidgets();
    initlization();
    initlizationSolts();
    updateLayout();
}

ShapePicker::~ShapePicker() { qDebug() << "~ShapePicker()"; }

void ShapePicker::createWidgets() {
    m_shapeContainer = new QWidget(this);
    m_shapeGroup = new QButtonGroup(m_shapeContainer);
}

void ShapePicker::initlization() {
    this->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    m_shapeContainer->setObjectName(QString::fromUtf8("shapeContainer"));
    // 使用迭代器遍历
    QMap<PainterType, IconFont>::const_iterator iter = m_shapes.constBegin();
    for (int i= 0 ; iter != m_shapes.constEnd(); ++iter,++i) {
        PainterType id = iter.key();
        IconFont fontCode = iter.value();
        ShapeItem *btn = new ShapeItem(id,m_shapeContainer);
//        QToolButton *btn = new QToolButton(m_shapeContainer);
        m_shapeGroup->addButton(btn);
        btn->setObjectName(QString("shape_%1").arg(i));
        btn->setCheckable(true);

        bool ok = true;
        ushort num = QString::number((int)fontCode, 16).toUShort(&ok, 16);
        el::ELIconUtil::setIcon(btn, QChar(num),"#666",18);


        if (DrawContext::painterType() == id) {
            btn->setChecked(true);
        }

        m_shapeItems.append(btn);
        connect(btn, &ShapeItem::clicked, this, &ShapePicker::onSelectShape);
    }
}

void ShapePicker::initlizationSolts() {
}


void ShapePicker::updateLayout() {
    m_mainLayout = new QVBoxLayout();
    SET_LAYOUT_MARGIN(m_mainLayout,10);
    m_mainLayout->setSpacing(0);
    setLayout(m_mainLayout);

    m_mainLayout->addWidget(m_shapeContainer);

    QGridLayout * shapeLayout = new QGridLayout(m_shapeContainer);
    SET_LAYOUT_MARGIN(shapeLayout,4);
    shapeLayout->setSpacing(10);

    for(int i = 0 ,row = 0,column = 0; i < m_shapeItems.size(); ++i,++column){
        QToolButton * btn = m_shapeItems[i];
        if(column == 4){
            ++row;
            column = 0;
        }
        shapeLayout->addWidget(btn,row,column);
    }
}

ShapePicker& ShapePicker::showPicker( QWidget *parent,
                                     ELPosition position, const QSize &size) {
    ShapePicker * pop = new ShapePicker(parent);
    pop->showPop(parent, position,size);
    return *pop;
}

void ShapePicker::showPop(QWidget *parent,
                          ELPosition position, const QSize &size) {
    m_position = position;
    QPoint point = getShowPosition(parent, size, position);

    QRect rcStart, rcEnd;
    rcStart = QRect(point.x() , point.y()- 50, width(), height());
    rcEnd = QRect(point.x(), point.y(), width(), height());
    show();
    QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
    animation->setDuration(100);
    animation->setStartValue(rcStart);
    animation->setEndValue(rcEnd);
    animation->start(QAbstractAnimation::DeleteWhenStopped);
}


void ShapePicker::onSelectShape() {
    ShapeItem *btn = static_cast<ShapeItem *>(sender());
    btn->setChecked(true);
    DrawContext::setPainterType(btn->getShapeType());
    emit shapeSelected(btn->getShapeType());
    close();
}

